<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>个人信息 - 教学管理系统</title>
    <style>
        body {
            font-family: 'Microsoft YaHei', sans-serif;
            background-color: #f5f5f5;
            margin: 0;
            padding: 0;
        }

        .container {
            display: flex;
            min-height: 100vh;
        }

        .sidebar {
            width: 250px;
            background-color: #2c3e50;
            color: white;
            padding-top: 20px;
        }

        .sidebar-header {
            padding: 0 20px 20px;
            border-bottom: 1px solid #3c5166;
        }

        .sidebar-header h2 {
            margin: 0;
            font-size: 20px;
        }

        .user-info {
            margin-top: 10px;
            font-size: 14px;
            opacity: 0.8;
        }

        .menu {
            list-style: none;
            padding: 0;
            margin: 20px 0;
        }

        .menu li {
            padding: 12px 20px;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        .menu li:hover {
            background-color: #3c5166;
        }

        .menu li.active {
            background-color: #3498db;
        }

        .menu a {
            color: white;
            text-decoration: none;
            display: block;
        }

        .content {
            flex: 1;
            padding: 20px;
        }

        .content-header {
            margin-bottom: 30px;
        }

        .content-header h1 {
            margin: 0;
            color: #333;
            font-size: 24px;
        }

        .profile-container {
            display: flex;
            gap: 30px;
        }

        .profile-sidebar {
            width: 250px;
        }

        .profile-avatar {
            background-color: white;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            padding: 20px;
            text-align: center;
            margin-bottom: 20px;
        }

        .avatar-img {
            width: 150px;
            height: 150px;
            border-radius: 50%;
            object-fit: cover;
            border: 5px solid #f0f0f0;
        }

        .avatar-upload {
            margin-top: 15px;
        }

        .avatar-upload input {
            display: none;
        }

        .avatar-upload label {
            display: inline-block;
            padding: 8px 16px;
            background-color: #3498db;
            color: white;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
        }

        .profile-stats {
            background-color: white;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            padding: 20px;
        }

        .stat-item {
            display: flex;
            justify-content: space-between;
            padding: 10px 0;
            border-bottom: 1px solid #eee;
        }

        .stat-item:last-child {
            border-bottom: none;
        }

        .stat-label {
            color: #666;
        }

        .stat-value {
            font-weight: 500;
            color: #333;
        }

        .profile-main {
            flex: 1;
        }

        .profile-section {
            background-color: white;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            padding: 20px;
            margin-bottom: 20px;
        }

        .profile-section h2 {
            margin-top: 0;
            margin-bottom: 20px;
            color: #333;
            font-size: 18px;
            border-bottom: 1px solid #eee;
            padding-bottom: 10px;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            color: #555;
            font-weight: 500;
        }

        .form-group input,
        .form-group select,
        .form-group textarea {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 14px;
            color: #333;
        }

        .form-group textarea {
            height: 100px;
            resize: vertical;
        }

        .form-row {
            display: flex;
            gap: 20px;
        }

        .form-row .form-group {
            flex: 1;
        }

        .btn {
            display: inline-block;
            padding: 10px 20px;
            background-color: #3498db;
            color: white;
            border-radius: 4px;
            text-decoration: none;
            font-size: 14px;
            border: none;
            cursor: pointer;
        }

        .btn:hover {
            background-color: #2980b9;
        }

        .btn-save {
            background-color: #27ae60;
        }

        .btn-save:hover {
            background-color: #219955;
        }

        .btn-password {
            background-color: #f39c12;
        }

        .btn-password:hover {
            background-color: #e67e22;
        }

        .notification {
            padding: 15px;
            margin-bottom: 20px;
            border-radius: 4px;
            display: none;
        }

        .notification.success {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }

        .notification.error {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }

        .logout {
            margin-top: auto;
            padding: 20px;
        }

        .logout a {
            color: #e74c3c;
            text-decoration: none;
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="sidebar">
            <div class="sidebar-header">
                <h2>教学管理系统</h2>
                <div class="user-info" th:if="${session.user}">
                    <span th:text="${session.user.username}">teacher1</span> - 教师
                </div>
            </div>
            <ul class="menu">
                <li><a href="/teacher/dashboard">我的概览</a></li>
                <li><a href="/teacher/courses">我的课程</a></li>
                <li><a href="/teacher/students">我的学生</a></li>
                <li><a href="/teacher/grades">成绩管理</a></li>
                <li class="active"><a href="/teacher/profile">个人信息</a></li>
            </ul>
            <div class="logout">
                <a href="/logout">退出登录</a>
            </div>
        </div>

        <div class="content">
            <div class="content-header">
                <h1>个人信息</h1>
            </div>

            <div id="notification" class="notification"></div>

            <div class="profile-container">
                <div class="profile-sidebar">
                    <div class="profile-avatar">
                        <img th:src="${teacher.avatar != null ? teacher.avatar : '/images/default-avatar.svg'}"
                            src="/images/p1.svg" alt="头像" class="avatar-img">
                        <div class="avatar-upload">
                            <input type="file" id="avatar-input" accept="image/*">
                            <label for="avatar-input">更换头像</label>
                        </div>
                    </div>

                    <div class="profile-stats">
                        <div class="stat-item">
                            <div class="stat-label">教师编号</div>
                            <div class="stat-value" th:text="${teacher.teacherId}">T20230001</div>
                        </div>
                        <div class="stat-item">
                            <div class="stat-label">职称</div>
                            <div class="stat-value" th:text="${teacher.title}">副教授</div>
                        </div>
                        <div class="stat-item">
                            <div class="stat-label">所属院系</div>
                            <div class="stat-value" th:text="${teacher.department}">计算机学院</div>
                        </div>
                        <div class="stat-item">
                            <div class="stat-label">课程数</div>
                            <div class="stat-value" th:text="${teacher.courseCount}">3</div>
                        </div>
                        <div class="stat-item">
                            <div class="stat-label">学生数</div>
                            <div class="stat-value" th:text="${teacher.studentCount}">75</div>
                        </div>
                    </div>
                </div>

                <div class="profile-main">
                    <div class="profile-section">
                        <h2>基本信息</h2>
                        <form id="profile-form" onsubmit="updateProfile(event)">
                            <div class="form-row">
                                <div class="form-group">
                                    <label for="name">姓名</label>
                                    <input type="text" id="name" name="name" th:value="${teacher.name}" required>
                                </div>
                                <div class="form-group">
                                    <label for="gender">性别</label>
                                    <select id="gender" name="gender">
                                        <option value="男" th:selected="${teacher.gender == '男'}">男</option>
                                        <option value="女" th:selected="${teacher.gender == '女'}">女</option>
                                    </select>
                                </div>
                            </div>

                            <div class="form-row">
                                <div class="form-group">
                                    <label for="email">邮箱</label>
                                    <input type="email" id="email" name="email" th:value="${teacher.email}">
                                </div>
                                <div class="form-group">
                                    <label for="phone">电话</label>
                                    <input type="tel" id="phone" name="phone" th:value="${teacher.phone}">
                                </div>
                            </div>

                            <div class="form-group">
                                <label for="office">办公室</label>
                                <input type="text" id="office" name="office" th:value="${teacher.office}">
                            </div>

                            <div class="form-group">
                                <label for="bio">个人简介</label>
                                <textarea id="bio" name="bio" th:text="${teacher.bio}"></textarea>
                            </div>

                            <button type="submit" class="btn btn-save">保存信息</button>
                        </form>
                    </div>

                    <div class="profile-section">
                        <h2>账号安全</h2>
                        <form id="password-form" onsubmit="changePassword(event)">
                            <div class="form-group">
                                <label for="old-password">当前密码</label>
                                <input type="password" id="old-password" name="oldPassword" required>
                            </div>

                            <div class="form-row">
                                <div class="form-group">
                                    <label for="new-password">新密码</label>
                                    <input type="password" id="new-password" name="newPassword" required>
                                </div>
                                <div class="form-group">
                                    <label for="confirm-password">确认新密码</label>
                                    <input type="password" id="confirm-password" name="confirmPassword" required>
                                </div>
                            </div>

                            <button type="submit" class="btn btn-password">修改密码</button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function () {
            console.log('教师个人信息页面加载完成');

            // 头像上传
            document.getElementById('avatar-input').addEventListener('change', uploadAvatar);
        });

        // 显示通知
        function showNotification(message, type) {
            const notification = document.getElementById('notification');
            notification.textContent = message;
            notification.className = 'notification ' + type;
            notification.style.display = 'block';

            // 5秒后自动隐藏
            setTimeout(function () {
                notification.style.display = 'none';
            }, 5000);
        }

        // 上传头像
        function uploadAvatar(event) {
            const file = event.target.files[0];
            if (!file) return;

            const formData = new FormData();
            formData.append('avatar', file);

            console.log('上传头像');

            fetch('/api/teacher/profile/avatar', {
                method: 'POST',
                body: formData,
                credentials: 'include'
            })
                .then(response => {
                    if (!response.ok) {
                        throw new Error('上传失败');
                    }
                    return response.json();
                })
                .then(data => {
                    console.log('头像上传成功:', data);

                    // 更新头像显示
                    const avatarImg = document.querySelector('.avatar-img');
                    avatarImg.src = data.avatarUrl + '?t=' + new Date().getTime(); // 添加时间戳防止缓存

                    showNotification('头像上传成功', 'success');
                })
                .catch(error => {
                    console.error('头像上传失败:', error);
                    showNotification('头像上传失败: ' + error.message, 'error');
                });
        }

        // 更新个人信息
        function updateProfile(event) {
            event.preventDefault();

            const formData = new FormData(document.getElementById('profile-form'));
            const profileData = Object.fromEntries(formData.entries());

            console.log('更新个人信息:', profileData);

            fetch('/api/teacher/profile/update', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify(profileData),
                credentials: 'include'
            })
                .then(response => {
                    if (!response.ok) {
                        throw new Error('更新失败');
                    }
                    return response.json();
                })
                .then(data => {
                    console.log('个人信息更新成功:', data);
                    showNotification('个人信息更新成功', 'success');
                })
                .catch(error => {
                    console.error('个人信息更新失败:', error);
                    showNotification('个人信息更新失败: ' + error.message, 'error');
                });
        }

        // 修改密码
        function changePassword(event) {
            event.preventDefault();

            const oldPassword = document.getElementById('old-password').value;
            const newPassword = document.getElementById('new-password').value;
            const confirmPassword = document.getElementById('confirm-password').value;

            if (newPassword !== confirmPassword) {
                showNotification('两次输入的新密码不一致', 'error');
                return;
            }

            console.log('修改密码');

            fetch('/api/teacher/profile/password', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({
                    oldPassword: oldPassword,
                    newPassword: newPassword
                }),
                credentials: 'include'
            })
                .then(response => {
                    if (!response.ok) {
                        throw new Error('密码修改失败');
                    }
                    return response.json();
                })
                .then(data => {
                    console.log('密码修改成功:', data);

                    // 清空表单
                    document.getElementById('password-form').reset();

                    showNotification('密码修改成功', 'success');
                })
                .catch(error => {
                    console.error('密码修改失败:', error);
                    showNotification('密码修改失败: ' + error.message, 'error');
                });
        }
    </script>
</body>

</html>